<template>
  <div class="dashboard-editor-container">
    <panel-group @handleSetLineChartData="handleSetLineChartData" />

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <line-chart />
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="16">
        <div class="chart-wrapper">
          <el-card shadow="hover">
            <el-table
              :data="mergedTableData"
              style="width: 100%"
              border
              stripe
            >
              <el-table-column
                prop="line_name"
                label="产线名称"
                width="180"
              />
              <el-table-column
                prop="daily"
                label="日产量"
                align="center"
              />
              <el-table-column
                prop="annual"
                label="年产量"
                align="center"
              />
            </el-table>
          </el-card>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <production-pie-chart
            title="产量分析"
            :chart-data="productionData"
          />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import PanelGroup from './dashboard/PanelGroup'
import LineChart from './dashboard/LineChart'
import RaddarChart from './dashboard/RaddarChart'
import PieChart from './dashboard/PieChart'
import BarChart from './dashboard/BarChart'
import ProductionPieChart from './dashboard/ProductionPieChart'
import { tableDayAndYear, ProductionPieChartYear, ProductionPieChartDay } from '@/api/mes/index'

const lineChartData = {
  newVisitis: {
    expectedData: [100, 120, 161, 134, 105, 160, 165],
    actualData: [120, 82, 91, 154, 162, 140, 145]
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130]
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130]
  },
  shoppings: {
    expectedData: [130, 140, 141, 142, 145, 150, 160],
    actualData: [120, 82, 91, 154, 162, 140, 130]
  }
}

export default {
  name: 'Index',
  components: {
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart,
    ProductionPieChart
  },
  data() {
    return {
      lineChartData: lineChartData.newVisitis,
      tableData: [],
      mergedTableData: [],
      productionData: {
        title: '产量分析',
        seriesData: []
      } // 初始化为一个空对象
    }
  },
  created() {
    this.getTableList()
    this.getProductionData()
  },
  methods: {
    getTableList() {
      tableDayAndYear().then(res => {
        this.mergedTableData = res
      })
    },
    getProductionData() {
      // 同时获取年产量和日产量数据
      Promise.all([
        ProductionPieChartYear(),
        ProductionPieChartDay()
      ]).then(([yearRes, dayRes]) => {
        // 确保 productionData 是一个对象
        this.productionData = {
          title: '产量分析',
          seriesData: [
            { value: yearRes, name: '年产量' },
            { value: dayRes, name: '日产量' }
          ]
        }
      }).catch(err => {
        console.error('获取产量数据失败:', err);
      })
    },
    handleSetLineChartData(type) {
      this.lineChartData = lineChartData[type]
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 10px;
  background-color: rgb(240, 242, 245);
  position: relative;
  margin: 5px;
  border-radius: 15px;
  .chart-wrapper {
    background: #fff;
    padding: 5px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
  .dashboard-editor-container {
    padding: 8px;
  }
}
</style>